package com.itbaizhan.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.itbaizhan.blockhandler.PaymentBlockHandler;
import com.itbaizhan.fallback.PaymentFallback;
import com.itbaizhan.service.GoodsService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentController {

    @Autowired
    private GoodsService goodsService;


    @GetMapping("/testA")
    public String testA(){
        return "testA";
    }


    /**
     * 慢调用
     * @param id
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/testC")
    public String testC(Integer id) throws InterruptedException {

        if (id == 1){
            Thread.sleep(2000);
        }

        return "testC";
    }


    /**
     * 异常比例
     * @param id
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/testD")
    public String testD(Integer id) throws InterruptedException {

        if (id == 1){
            throw new RuntimeException("故意抛出异常，触发异常比例熔断。");
        }

        return "testD";
    }


    /**
     * 异常数
     * @return
     */
    @GetMapping("/testF")
    public String testF(HttpServletRequest request){
       System.out.println(10/0);
        return "testF";
    }


    /**
     * 查询订单
     * @return
     */
    @GetMapping("/query")
    public String query(){
        goodsService.queryGoods();
        return "订单内容";
    }


    /**
     * 添加订单
     * @return
     */
    @GetMapping("/save")
    public String save(){
        goodsService.queryGoods();
        return "添加订单";
    }



    /**
     * 更新订单
     * @return
     */
    @GetMapping("/update")
    public String update(){
        return "更新订单";
    }


    /**
     * 预热
     * @return
     */
    @GetMapping("/warmup")
    public String warmup(){
        return "预热";
    }


    /**
     * 热点数据
     * @param id
     * @return
     */
    @SentinelResource("hot")
    @GetMapping("/findById")
    public Long getGoods(Long  id){
        return id;
    }


    /**
     * 授权规则
     * @return
     */
    @GetMapping("/auth")
    public String auth(){
        return "auth";
    }


    /**
     * 根据id查询支付信息
     * 内置  fallback
     * @param id
     * @return
     */
//    @SentinelResource(value = "testFallback",fallback = "testFallback")
//    @GetMapping("/payment/findById")
//    public String findById(Long id){
//        if( id == 1){
//            throw new RuntimeException("出异常了");
//        }
//        return "支付信息";
//    }


    /**
     * 根据id查询支付信息
     * 外置  fallback
     * @param id
     * @return
     */
    @SentinelResource(value = "findById",fallback = "findById",fallbackClass = PaymentFallback.class)
    @GetMapping("/payment/findById")
    public String findById(Long id){
        if( id == 1){
            throw new RuntimeException("出异常了");
        }
        return "支付信息";
    }

    /**
     * sentinel  问题处理
     * 内置
     * @param id
     * @return
     */
//    @SentinelResource(value = "block",blockHandler = "blockHandler")
//    @GetMapping("/block")
//    public String  block(Long id){
//
//        if (id == 1){
//            System.out.println(10/0);
//        }
//
//        return "block";
//
//    }


    /**
     * sentinel  问题处理
     * 外置
     * @param id
     * @return
     */
    @SentinelResource(value = "block",blockHandlerClass = PaymentBlockHandler.class,blockHandler = "blockHandler")
    @GetMapping("/block")
    public String  block(Long id){

        if (id == 1){
            System.out.println(10/0);
        }

        return "block";

    }

    //处理sentinel熔断异常
//    public String  blockHandler(Long id, BlockException e) {
//
//        return "服务繁忙稍等一会~~~~";
//
//    }



    // 处理java异常
    public String testFallback(Long id,Throwable e){
      return "服务繁忙稍等一会~~~~";
    }


    /**
     * 主要测试fallback 和 blockHandler
     * @param id
     * @return
     */
    @SentinelResource(value = "findByPaymenId",fallback = "fallbackHandler",blockHandler = "blockHandler")
    @GetMapping("findByPaymenId")
    public String findByPaymenId(Long id){

        if(id == 1){
            System.out.println(10/0);
        }

        return "支付记录查询成功";
    }

    // 处理java异常
    public String fallbackHandler(Long id,Throwable throwable){
        return "处理java异常";

    }

    // 处理sentienl 限流或者熔断
    public String blockHandler(Long id,BlockException e){

        return "处理sentienl";
    }


}
